{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 合规判定提示词 #\n",
    "from openai import OpenAI\n",
    "\n",
    "client = OpenAI(\n",
    "    #api_key=os.getenv(\"DASHSCOPE_API_KEY\"),\n",
    "    # api_key=\"sk-b571bfbe652b4ec68ac0491e33949622\", # 这种写法不好，泄露了api-key。 回头正式部署时改掉。\n",
    "    # base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "    api_key=\"ollama\",\n",
    "    base_url=\"http://192.168.20.43:11434/v1\"\n",
    ")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Question(question='谁是电话的发明者？', answer=None),\n",
      " Question(question='电话发明者的出生国是哪个国家？', answer=None),\n",
      " Question(question='该国的首都是哪里？', answer=None)]\n"
     ]
    }
   ],
   "source": [
    "def llm_query_decomposition(question):\n",
    "    system_prompt = f\"\"\"\\\n",
    "    You are a helpful assistant that prepares queries that will be sent to a search component.\n",
    "    Sometimes, these queries are very complex.\n",
    "    Your job is to simplify complex queries into multiple queries that can be answered\n",
    "    in isolation to eachother.\n",
    "\n",
    "    If the query is simple, then keep it as it is.\n",
    "    Examples\n",
    "    1. Query: Did Microsoft or Google make more money last year?\n",
    "    Decomposed Questions: [Question(question='How much profit did Microsoft make last year?', answer=None), Question(question='How much profit did Google make last year?', answer=None)]\n",
    "    2. Query: What is the capital of France?\n",
    "    Decomposed Questions: [Question(question='What is the capital of France?', answer=None)]\n",
    "    3. Query: {question}\n",
    "    Decomposed Questions:\n",
    "\n",
    "    # 限制条件 #\n",
    "    - Reply in Chinese.\n",
    "    \"\"\"\n",
    "    completion = client.chat.completions.create(\n",
    "        model=\"qwen2-7b-instruct\",\n",
    "        messages=[{'role': 'system', 'content': system_prompt},\n",
    "                  {'role': 'user', 'content': query}],\n",
    "        )\n",
    "    return completion.choices[0].message.content\n",
    "\n",
    "query = \"电话发明者出生国家的首都是哪？\"   \n",
    "llm_output = llm_query_decomposition(query) \n",
    "print(llm_output)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
